package com.ecfront.action;

import java.util.Map;
import java.util.regex.Pattern;

import com.ecfront.exception.UserException;
import com.ecfront.pojo.User;
import com.ecfront.service.UserService;
import com.ecfront.util.Constants;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class RegistAction extends ActionSupport {
	
	protected UserService userService;
	//用户封装类
	private String name;
	private String email;
	private String loginPwd;
	private String loginPwd2;
	private String payPwd;
	private String payPwd2;
	private String question;
	private String answer;
	private String vercode;

	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	//注册信息验证
	public void validateRegist() {
		clearErrorsAndMessages();//清除页面上错误信息
		Map sessions = ActionContext.getContext().getSession();
		String vercode2 = (String)sessions.get(Constants.LOGIN_CHECK_IMG);//获得验证码的session
		if(!vercode2.equalsIgnoreCase(vercode)){
			addFieldError("vercode", "验证码错误");
			sessions.put(Constants.LOGIN_CHECK_IMG, null);
		}else{
			sessions.put(Constants.LOGIN_CHECK_IMG, null);
			if(userService.findUserByEmail(email)){
				addFieldError("email", "该Email地址已存在");
			}
			if(userService.findUserByName(name)){
				addFieldError("name", "该用户已存在");
			}
			if(name!=null&&!Pattern.matches("^[a-z0-9\\u4e00-\\u9fa5]{2,20}$", name.trim())){
				addFieldError("name","您输入的用户名必须是4~25的中文、字母或数字");
			}
			if(email!=null&&!Pattern.matches( "\\w+(\\.\\w+)*@\\w+(\\.\\w+)+", email.trim())){
				addFieldError("email","您输入Email地址不是正确的地址");			
			}
			if(loginPwd!=null&&!Pattern.matches("\\w{6,20}", loginPwd.trim())){
				addFieldError("loginPwd", "您输入的登录密码必须由6~20位字母和数字组成");
			}
			if(!loginPwd.equals(loginPwd2)){
				addFieldError("loginPwd2", "您2次输入的登录密码不一致，请重新输入");
			}
			if(payPwd!=null&&!Pattern.matches("\\w{6,20}", payPwd.trim())){
				addFieldError("payPwd", "您输入的支付密码必须由6~20位字母和数字组成");
			}
			if(!payPwd.equals(payPwd2)){
				addFieldError("payPwd2", "您2次输入的支付密码不一致，请重新输入");
			}
			if(question!=null&&!Pattern.matches(".{2,40}", question.trim())){
				addFieldError("question", "您的问题不能超过40个字节");
			}
			if(answer!=null&&!Pattern.matches(".{2,40}", question.trim())){
				addFieldError("answer", "您的回答不能超过40个字节");
			}
		}
	}
	public void validate() {}
	//注册action
	public String regist() throws Exception {
		try{				
			userService.addUser(getName(), getEmail(), getLoginPwd(), 
					getPayPwd(), getQuestion(), getAnswer());
			return SUCCESS;
		}catch (UserException e) {
			addActionError("注册出现异常，请联系管理员");
			return INPUT;
		}
	}
	public String execute() throws Exception {
		return SUCCESS;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getLoginPwd() {
		return loginPwd;
	}
	public void setLoginPwd(String loginPwd) {
		this.loginPwd = loginPwd;
	}
	public String getPayPwd() {
		return payPwd;
	}
	public void setPayPwd(String payPwd) {
		this.payPwd = payPwd;
	}
	public String getQuestion() {
		return question;
	}
	public void setQuestion(String question) {
		this.question = question;
	}
	public String getAnswer() {
		return answer;
	}
	public void setAnswer(String answer) {
		this.answer = answer;
	}
	public String getLoginPwd2() {
		return loginPwd2;
	}
	public void setLoginPwd2(String loginPwd2) {
		this.loginPwd2 = loginPwd2;
	}
	public String getPayPwd2() {
		return payPwd2;
	}
	public void setPayPwd2(String payPwd2) {
		this.payPwd2 = payPwd2;
	}
	public String getVercode() {
		return vercode;
	}
	public void setVercode(String vercode) {
		this.vercode = vercode;
	}
}
